package com.js.admin.mapper;

import com.js.admin.entity.BaseElement;
import com.js.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author ygl
 * @create 2018-10-31
 * @DESCRIPTION
 **/
public interface BaseElementMapper extends BaseMapper<BaseElement> {

    /**
     * 获得所有有菜单的资源
     * 即：有menuId的资源
     * @return
     */
    @Select("select distinct t.code,t.type,t.name,t.uri,t.method,m.title as menu_id " +
            "from base_element t inner join base_menu m on t.menu_id = m.id")
    List<BaseElement> selectAllElementPermissions();

    /**
     * 查询租户拥有的所有资源
     * @param tenantId 租户ID
     * @param type
     * @return
     */
    @Select("select distinct t.code,t.type,t.name,t.uri,t.method,m.title as menu_id from base_resource_authority ra " +
            "   inner join base_element t on ra.resource_id = t.id and ra.authority_id in " +
            "   ( select group_id from base_app_group where app_id in (select app_id from base_app_order where tenant_id = #{tenantId}) )" +
            "   and ra.authority_type = 'group' and ra.resource_type = 'button' and ra.type = #{type} " +
            "    inner join base_menu m on t.menu_id = m.id")
    List<BaseElement> selectAuthorityElementByTenantId(@Param("tenantId") String tenantId, @Param("type") String type);

    @Select("select distinct t.code,t.type,t.name,t.uri,t.method,m.title as menu_id from base_resource_authority ra" +
            "   inner join base_element t on ra.resource_id = t.id and ra.authority_id in " +
            "   ( select group_id from base_group_member where user_id = #{userId} " +
            "       union select group_id from base_group_leader where user_id = #{userId}" +
            "       union select bpg.group_id from base_position_group bpg " +
            "       inner join base_position_user bpu on bpg.position_id = bpu.position_id and bpu.user_id = #{userId}" +
            "   ) " +
            "   and ra.authority_type = 'group' and ra.resource_type = 'button' and ra.type = #{type}" +
            "   inner join base_menu m on t.menu_id = m.id")
    List<BaseElement> selectAuthorityElementByUserId(@Param("userId") String userId, @Param("type") String type);


    //    @Select("select distinct t.* from base_resource_authority ra inner join base_element t " +
//            " on ra.resource_id = t.id and ra.authority_id in " +
//            " (select group_id from base_app_group where app_id in " +
//            "   (select app_id from base_app_order where tenant_id = #{tenantId})" +
//            "  ) " +
//            " and ra.authority_type = 'group' and ra.resource_type = 'button' and ra.type = #{type} and t.menu_id = #{menuId}")
    List<BaseElement> selectAuthorityMenuElementByTenantId(@Param("tenantId") String tenantId, @Param("menuId") String menuId, @Param("type") String type);

//    @Select("select distinct t.* from base_resource_authority ra inner join base_element t" +
//            " on ra.resource_id = t.id and ra.authority_id in " +
//            " ( select group_id from base_group_member where user_id = #{userId} " +
//            "  union select group_id from base_group_leader where user_id = #{userId} " +
//            "  ) " +
//            " and ra.authority_type = 'group' and ra.resource_type = 'button' and ra.type = #{type} and t.menu_id = #{menuId}")
    List<BaseElement> selectAuthorityMenuElementByUserId(@Param("userId") String userId, @Param("menuId") String menuId, @Param("type") String type);


}
